﻿#define _CRT_SECURE_NO_WARNINGS
// ↑ 将本行在代码放在第一行可以避免 scanf的报错问题（仅在VS编译器里）

#include"BinaryTree.h"
#include"Queue.h"


//【函数定义】
/*前序遍历*/void PrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("#");
		return;
	}
	printf("%c", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);

}
/*中序遍历*/void MidOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	MidOrder(root->left);
	printf("%c ", root->data);
	MidOrder(root->right);
}
/*后序遍历*/void NextOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	NextOrder(root->left);
	NextOrder(root->right);
	printf("%d ", root->data);
}
/*二叉树计算大小*/int TreeSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	else
	{
		return  1 + TreeSize(root->left) + TreeSize(root->right);
	}
}
/*二叉树计算叶子节点数量*/int TreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return   1;
	}
	else
	{
		return (TreeLeafSize(root->left) + TreeLeafSize(root->right));
	}
}
/*二叉树计算深度*/int TreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int rd = TreeDepth(root->right);
	int ld = TreeDepth(root->left);
	return rd > ld ? rd + 1 : ld + 1;
}
/*根据前序生成原树*/BTNode* gettreebyprevprint(char* prev,int* i)
{

	if (prev[*i] == '#')
	{ 
		++(*i);
		return NULL;
	}
	else
	{
		BTNode* root = (BTNode*)malloc(sizeof(BTNode));
		root->data = prev[*i];
		++(*i);
		root->left = gettreebyprevprint(prev, i);
		root->right = gettreebyprevprint(prev, i);
		return root;
	}
}
/*二叉树第K层节点个数*/int TreeLevelKSize(BTNode* root, int k)//第k层的节点数等于左右子树的第k-1层，递归直至k=1
{
	if (root == NULL)return 0;
	if (k == 1)
	{
		
		return 1;
	}
	return TreeLevelKSize(root->left, k - 1) + TreeLevelKSize(root->right, k - 1);
}
/*二叉树查找值为x的节点*/BTNode* TreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
	{
		return NULL;
	}
	if (root->data == x)
	{
		return root;
	}
	BTNode* node = TreeFind(root->left, x);
	if (node != NULL)
		return node;
	node = TreeFind(root->right, x);
	if (node != NULL)
		return node;
	return NULL;
}
/*二叉树销毁*/void DestoryTree(BTNode* root)
{
	if (root == NULL)return;
	
	DestoryTree(root->left);
	DestoryTree(root->right);
	free(root);
	root = NULL;
}
/*二叉树层序遍历*/void TreeLevelOrder(BTNode* root)
{
	Que tmpq;
	QueueInit(&tmpq);
	if (root == NULL)
	{
		//printf("NULL ");
		QueueDestroy(&tmpq);
		return;
	}
	
	QueuePush(&tmpq, root);
	while (!QueueEmpty(&tmpq))
	{
		BTNode* front = QueueFront(&tmpq);
		QueuePop(&tmpq);
		printf("%c ",front->data);
		if (front->left)
		{
			QueuePush(&tmpq, front->left);
		}
		if (front->right)
		{
			QueuePush(&tmpq, front->right);
		}
	
	}
	printf("\n");
	QueueDestroy(&tmpq);
}
/*二叉树判断是否完全*/int TreeComplete(BTNode* root)
{
	Que tmpq;
	QueueInit(&tmpq);
	if (root == NULL)
	{
		//printf("NULL ");
		QueueDestroy(&tmpq);
		return 1;
	}

	QueuePush(&tmpq, root);
	while (!QueueEmpty(&tmpq))
	{
		BTNode* front = QueueFront(&tmpq);
		QueuePop(&tmpq);
		if (front == NULL)break;
		QueuePush(&tmpq, front->left);
		QueuePush(&tmpq, front->right);
		
	}
	while (!QueueEmpty(&tmpq))
	{
		BTNode* front = QueueFront(&tmpq);
		QueuePop(&tmpq);
		if (front)
		{
			QueueInit(&tmpq);
			return 0;
		}
	}
	QueueDestroy(&tmpq);
	return 1;
}
